<template>
  <div>

    <el-divider content-position="left">重新生成的源码，将覆盖原有源码，请备份好，再执行！！！</el-divider>
    <el-button-group style="margin-left: 40px;">
      <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(5, false)"  style="width:360px; text-align: left; margin-top: 10px;">Vue:只生成tmp临时文件提供参考,参考完删除tmp文件夹</el-button>
      <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(5, true)"  style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>

    <br/>
    <el-button-group style="margin-left: 40px;">
      <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(3, false)"  style="width:360px; text-align: left; margin-top: 10px;">只生成后端通用模板部分</el-button>
      <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(3, true)"  style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>
    <br/>
    <br/>
    <br/>
    <el-divider content-position="left">轻易别点我，会覆盖后端自定义代码部分</el-divider>
    <el-button-group style="margin-left: 40px;">
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(1, false)" style="width:360px; text-align: left; margin-top: 10px;">只生成Vue自定义部分</el-button>
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(1, true)" style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>
    <br/>
    <el-button-group style="margin-left: 40px;">
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(4, false)" style="width:360px; text-align: left; margin-top: 10px;">只生成后端自定义部分</el-button>
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(4, true)" style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>
    <br/>
    <el-button-group style="margin-left: 40px;">
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(2, false)" style="width:360px; text-align: left; margin-top: 10px;">生成后端所有</el-button>
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(2, true)" style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>
    <br/>
    <el-button-group style="margin-left: 40px;">
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-setting" @click="handleCreateBuildCode4Row(0, false)" style="width:360px; text-align: left; margin-top: 10px;">生成前后端所有（初始化）</el-button>
    <el-button class="filter-item" size="small" type="primary" icon="el-icon-download" @click="handleCreateBuildCode4Row(0, true)" style="margin-top: 10px;">生成并下载</el-button>
    </el-button-group>
    <br/>
    <br/>
    <br/>
  </div>
</template>

<!--
All(0, "生成所有"),
Vue(1, "前端"),
ServiceAll(2, "后端所有"),
ServiceTmpt(3, "后端通用模板部分"),
ServiceMyself(4, "后端自定义部分");
-->
<script>
import request from '@/utils/request'
import {gen2LevelMenuApi} from './tmpt-list-api'
import {getToken} from "@/utils/auth";

export default {
  name: 'GenCode',
  props: {
    keyRandom: String,
    tmptId: Number,
    tmptTableName: String
  },
  data() {
    return {
      coding_menu_id: 668,

    }
  },

  methods: {
    // 重新生成源码
    handleCreateBuildCode4Row(genType, downloadZip) {
      const params = {
        'menu1Id': this.coding_menu_id,
        '_id': this.tmptId,
        'pageType': 'datagrid-sourcecode',
        'menu2Name': this.tmptTableName,
        'keyRandom': this.keyRandom,
        'genType':genType,
        'downloadZip': downloadZip
      }
      /*gen2LevelMenuApi(params).then(r => {
        this.notify(r)
      })*/

      if (downloadZip) {
        request({
          url: '/core/admin123/mpgenerator/template2menu',
          method: 'post',
          params,
          responseType: 'arraybuffer' //此处注意请求头
        }).then(res => {
          let fileName = 'easyjava.zip' //此处获取文件名称
          this.download(res.data, fileName) //此处跳转到第三步
        })
      }else {
        request({
          url: '/core/admin123/mpgenerator/template2menu',
          method: 'post',
          params
        }).then(r => {
          this.notify(r)
        })
      }

    },

    download (data,fileName) {
      if (!data) {
        return
      }
      let url = window.URL.createObjectURL(new Blob([data])) //创建下载链接
      let link = document.createElement('a') //创建a标签
      link.style.display = 'none'  //将a标签隐藏
      link.href = url  //给a标签添加下载链接
      link.setAttribute('download', fileName) // 此处注意，要给a标签添加一个download属性，属性值就是文件名称 否则下载出来的文件是没有属性的，空白白
      document.body.appendChild(link)
      link.click()  //执行a标签
    }



  }
}
</script>

<style scoped>

</style>
